package com.shenmazong.servereduschool.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenmazong.servereduschool.pojo.TbStage;
import com.shenmazong.servereduschool.service.TbStageService;
import com.shenmazong.servereduschool.mapper.TbStageMapper;
import com.shenmazong.utils.ResultResponse;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbStageServiceImpl extends ServiceImpl<TbStageMapper, TbStage>
implements TbStageService{
    @Autowired
    TbStageMapper tbStageMapper;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public PageInfo findStageList(Integer pageNum, Integer pageSize, Object tbStage) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbStage> tbStages=tbStageMapper.findStageList(tbStage);
        PageInfo<TbStage> pageInfo = new PageInfo<>(tbStages);
        return pageInfo;
    }

    @Override
    public ResultResponse add(TbStage tbStage) {
        try {
            tbStage.setCreateTime(new Date());
            tbStage.setUpdateTime(new Date());
            int i = tbStageMapper.insert(tbStage);
            elasticsearchRestTemplate.save(tbStage);
            if(i<1){
                return ResultResponse.FAILED(-1,"添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse queryByStageId(TbStage tbStage) {
        try {
            TbStage stage = tbStageMapper.selectById(tbStage.getStageId());
            return ResultResponse.SUCCESS(stage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }

    }

    @Override
    public ResultResponse updateStage(TbStage tbStage) {
        try {
            tbStage.setUpdateTime(new Date());
            int i = tbStageMapper.updateById(tbStage);
            if(i<1){
                return ResultResponse.FAILED("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse delByStageId(TbStage tbStage) {
        try {
            tbStageMapper.deleteById(tbStage.getStageId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

//    @Scheduled(fixedRate = 60000L)
//    public void dingshi(){
//        System.out.println("60秒执行一次");
//        List<TbStage> tbStages = tbStageMapper.selectList(null);
//        if(tbStages.size()>0){
//            elasticsearchRestTemplate.save(tbStages);
//        }
//    }

    @Override
    public ResultResponse findStage(Integer pageNum, Integer pageSize, TbStage tbStage) {
        ArrayList<TbStage> stages = new ArrayList<>();

        //模糊
        if(tbStage.getStageCourseName()!=null){
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.field("stageCourseName").preTags("<b><span style='color:red'>").postTags("</span></b>");

            QueryBuilder query = QueryBuilders.multiMatchQuery(tbStage.getStageCourseName());
            NativeSearchQuery builder = new NativeSearchQueryBuilder()
                    .withHighlightBuilder(highlightBuilder)
                    .withQuery(query)
                    .build();
            SearchHits<TbStage> search = elasticsearchRestTemplate.search(builder, TbStage.class);
            for (SearchHit<TbStage> hit : search.getSearchHits()) {
                List<String> stageCourseName = hit.getHighlightField("stageCourseName");
                if(stageCourseName!=null){
                    hit.getContent().setStageCourseName(stageCourseName.get(0));
                    stages.add(hit.getContent());
                }
            }
            return ResultResponse.SUCCESS(stages);
        }


        SearchHits<TbStage> search = elasticsearchRestTemplate.search(Query.findAll(), TbStage.class);
        for (SearchHit<TbStage> hit : search.getSearchHits()) {
            stages.add(hit.getContent());
        }
        return ResultResponse.SUCCESS(stages);
    }

    @Override
    public ResultResponse delAll(List<TbStage> ids) {
        if(ids.size()<=0){
            return ResultResponse.FAILED(-1,"至少选择一条数据");
        }
        for (TbStage stage : ids) {
            tbStageMapper.deleteById(stage.getStageId());
        }
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse findTbStageList() {
        List<TbStage> tbStages = tbStageMapper.selectList(null);
        return ResultResponse.SUCCESS(tbStages);
    }


}




